package com.sx.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.model.Msg;
import com.dfec.utils.Consists;
import com.dfec.utils.ResponseUtil;
import com.sx.service.NotifacationSendService;


@WebServlet(name = "n_s_s", description = "通知下发", urlPatterns = { "/n_s_s" })
public class NotifacationSendServlet extends HttpServlet {
	private List<Object> list=null;
    Msg msg=null;
	NotifacationSendService notifacationSendService = new NotifacationSendService();
	private static final long serialVersionUID = 1L;
	private String type;
	private int page;
	private int size;
	private int not_id;
	private String phone;
	private String name;
	private String city;
	private String country;
	private String town;
	private String title;
	private String content;
	private int hours;
	private String post;

	public NotifacationSendServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		msg=new Msg();
		list=new ArrayList<>();
		type = request.getParameter("type");
	try {
		if(type.equals("list")) {
			phone = request.getParameter("phone");
			page = Integer.parseInt(request.getParameter("page"));
			size = Integer.parseInt(request.getParameter("size"));
			town = request.getParameter("town");
			List<Object> getdata =notifacationSendService.getListData(page, size, town, phone);
			if(!getdata.get(0).toString().equals("[]")){
				msg.setCode(Consists.DATA_Code_OK);
				msg.setMsg(Consists.DATA_Msg_OK);
			}else {
				msg.setCode(Consists.DATA_Code_OK__NODATA);
				msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
			}
			list.add(msg);
			list.add(getdata.get(0));
		}else if(type.equals("detail")){
			not_id = Integer.parseInt(request.getParameter("not_id"));
			phone = request.getParameter("phone");
			name = request.getParameter("name");
			city = request.getParameter("city");
			country = request.getParameter("country");
			town = request.getParameter("town");
			List<Object> getdata =notifacationSendService.getContent(phone, name, city, country, town, not_id);
			if(!getdata.get(0).toString().equals("[]")){
				msg.setCode(Consists.DATA_Code_OK);
				msg.setMsg(Consists.DATA_Msg_OK);
			}else {
				msg.setCode(Consists.DATA_Code_OK__NODATA);
				msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
			}
			list.add(msg);
			list.add(getdata.get(0));
		
		}else if(type.equals("add")){
			phone = request.getParameter("phone");
			title = request.getParameter("title");
			content = request.getParameter("content");
			hours = Integer.parseInt(request.getParameter("hours"));
			city = request.getParameter("city");
			country = request.getParameter("country");
			town = request.getParameter("town");
			post = request.getParameter("post");
			name = request.getParameter("name");
			int getdata=notifacationSendService.commitSend(title, content, hours, city, country, town, phone, name, post);
			if(getdata>0){
				msg.setCode(Consists.DATA_Code_OK);
				msg.setMsg(Consists.DATA_Msg_OK);
			}else {
				msg.setCode(Consists.DATA_Code_OK__NODATA);
				msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
			}
			list.add(msg);
			list.add(new ArrayList<>());	
		}
		
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
		msg.setCode(Consists.Error_Code_ServerError);
		msg.setMsg(Consists.Error_Msg_ServerError);
		list.add(msg);
		list.add(new ArrayList<>());
	}
		ResponseUtil.sendScript(response,JSON.toJSONString(list));
	}
}
